# views.py : 路由 + 视图函数

from flask import Blueprint, request, jsonify
from .models import *

# 蓝图
blue = Blueprint('user', __name__)
blue2 = Blueprint('product', __name__)


@blue.route('/')
def index():
    return 'index'


@blue2.route('/goods')
def product():
    return 'product'


# request : 请求
@blue.route('/req', methods=['GET', 'POST'])
def api_req():
    print('url: ', request.url)
    print('path: ', request.path)
    print('host: ', request.host)
    print('method: ', request.method)
    print('base_url: ', request.base_url)
    print('remote_addr: ', request.remote_addr)
    print('remote_user: ', request.remote_user)
    print('files: ', request.files)
    print('headers: ', request.headers)
    print('user_agent: ', request.user_agent)
    # GET
    print(request.args)  # get 请求 , ImmutableMultiDict([('name', 'wxg'), ('name', '小明')])
    # print('param name: ', request.args.get('name'))
    # print('param name list: ', request.args.getlist('name'))
    # POST
    print(request.form)  # post 请求 , ImmutableMultiDict([('username', 'wxg'), ('age', '18')])
    # print(request.form.get('name'))
    # cookie
    print(request.cookies)  # ImmutableMultiDict([('cookie', 'xxx__yy_z')])

    return 'hello flask request'


# response : 响应
@blue.route('/resp')
def api_resp():
    # 响应的几种方式
    # 1、 字符串 2、模板 render_template 3、 json  4、自定义 make_response / Response
    json_data = {'name': '张三', 'age': 25}
    return jsonify(json_data)

